package us.mmapp;

import java.util.ArrayList;

import us.mmapp.util.ApplicationData;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TableRow.LayoutParams;

public class FilterActivity extends Activity implements OnClickListener{
	
	private ArrayList<Spinner> spinnerArray;
	private ArrayList<EditText> filterTextArray;
	
    protected int mSelection[];
    protected String mKeyword[];
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.filter_view);
		
		
		
		spinnerArray=new ArrayList<Spinner>();
		filterTextArray=new ArrayList<EditText>();
		
		Button filterButton = (Button) findViewById(R.id.filter_ok);
		
		final FilterActivity f=this;
		filterButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mSelection=new int[spinnerArray.size()];
				mKeyword=new String[spinnerArray.size()];
				
				boolean valid=true;
				for(int i=0; i<spinnerArray.size(); i++)
				{
					String word=filterTextArray.get(i).getText().toString();
					mSelection[i]=spinnerArray.get(i).getSelectedItemPosition();
					mKeyword[i]=word;
					if(word==null || word.equals(""))
					{
						valid=false;
					}
				}
				if(valid)
					FilterOn();
				else
					InfoActivity.alertPopup("Invalid Text", "You can not leave a text field empty!", f);
			}
		});
		
		Button cancelButton = (Button) findViewById(R.id.filter_cancel);
		
		cancelButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				FilterOff();
			}
		});
		
		Button addFilter=(Button)findViewById(R.id.filter_add);
		addFilter.setOnClickListener(this);
		Button removeFilter=(Button)findViewById(R.id.filter_remove);
		removeFilter.setOnClickListener(this);
		if(ApplicationData.getSelection()!=null)
		{
			mSelection=ApplicationData.getSelection();
			mKeyword=ApplicationData.getKeyword();
			for(int i=0; i<mSelection.length; i++)
			{
				addFilterField(mSelection[i], mKeyword[i]);
			}
		}
		else
			addFilterField(-1, null);
	}
	
	public void FilterOn()
	{	
		ApplicationData.setSelection(mSelection);
		ApplicationData.setKeyword(mKeyword);
		Intent intent = new Intent();
		intent.putExtra("type", mSelection);
		intent.putExtra("keyword", mKeyword);
		this.setResult(RESULT_OK, intent);
		this.finish();
	}
	
	public void FilterOff()
	{
		this.setResult(RESULT_CANCELED);
		this.finish();
	}

	@Override
	public void onClick(View v)
	{
		if(v.getId()==R.id.filter_add)
		{
			addFilterField(-1, null);
		}
		else if(spinnerArray.size()>1)
		{
			LinearLayout layout=(LinearLayout)findViewById(R.id.filter_add_layout);
			layout.removeView(spinnerArray.remove(spinnerArray.size()-1));
			layout.removeView(filterTextArray.remove(filterTextArray.size()-1));
		}		
	}
	private void addFilterField(int prevSelection, String keyword)
	{
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.filter_types,
                android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		LinearLayout layout=(LinearLayout)findViewById(R.id.filter_add_layout);
		
		Spinner s=new Spinner(this);
		s.setAdapter(adapter);
		s.setPrompt("Filter Type");
		EditText e=new EditText(this);
		e.setHint("Keyword");
		
		LayoutParams lp=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		s.setLayoutParams(lp);
		e.setLayoutParams(lp);
		if(prevSelection!=-1 || keyword!=null)
		{
			s.setSelection(prevSelection);
			e.setText(keyword);
		}
		
		spinnerArray.add(s);
		filterTextArray.add(e);
		
		layout.addView(s);
		layout.addView(e);
	}
}
